﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Serialization.Formatters;
using System.Text;

namespace Server
{
    class Program
    {
        private const int PORT = 8080;

        private static void InitializeChannel()
        {
            BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();
            BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();
            serverProvider.TypeFilterLevel = TypeFilterLevel.Full;

            IDictionary props = new Hashtable();
            props["port"] = PORT;
            TcpChannel channel = new TcpChannel(props, clientProvider, serverProvider);
            ChannelServices.RegisterChannel(channel);
        }

        static void Main(string[] args)
        {
//            TcpChannel channel = new TcpChannel(PORT);
//            ChannelServices.RegisterChannel(channel, true);
            InitializeChannel();

            BusinessService businessService = new BusinessService();
            ChatService chatService = new ChatService();

            RemotingServices.Marshal(businessService, "BusinessService");
            RemotingServices.Marshal(chatService, "ChatService");

            Console.WriteLine("runing, Port:{0}", PORT);

            string cmd = string.Empty;
            while (true)
            {
                if (cmd.Equals("m"))
                {
                    chatService.GetRecentMessages().ForEach(Console.WriteLine);
                }
                else if(cmd.StartsWith("s:"))
                {
                    string message = cmd.Substring(2);
                    //chatService.Broadcasting(message);
                    chatService.SendBroadcastMessage(message);
                }
                cmd = Console.ReadLine();
            }
        }
    }
}
